from app import app,Models,index_Handle
from tasks import add
from app.project import exib_Original
from flask import render_template, session, redirect, url_for
from app.Interface_mysql import user, center_u, group, Images_page, Work_station, Thumb_sp,Implement
from flask import request, jsonify
from PIL import Image
import os
import celery
# from celery import Celery
import time
from flask_admin.contrib.sqla import ModelView
from flask_admin import BaseView, expose
# 爬虫原创
def Yuan(id):
    rote = os.path.dirname(__file__)
    vv = Work_station(Type=id).Check()
    if vv is False:
        mm = exib_Original.AA(rote)
    else:
        mm = exib_Original.AA(rote,vv)
    if mm !=[]:
        for i in mm:
            name = i["歌名"]
            singer = i["演唱："]
            Lyrics = i["作词："]
            Composer = i["作曲："]
            Arranger = i["编曲："]
            Mix = i["混缩："]
            Lyric_ci = i["歌词"]
            Type = 1
            Address = i["地址"]
            if i["图片地址"] is None:
                photo = "images/moves/none_images.jpg"
            else:
                photo = i["图片地址"]
            time = str(i["上传时间："]).replace("上传时间：", "")
            Work_station(name=name, singer=singer, Lyrics=Lyrics,
                         Composer=Composer, Arranger=Arranger, Mix=Mix,
                         Lyric_ci=Lyric_ci, Type=Type, Address=Address, time=time,
                         photo=photo).Increase()

def Fan(id):
    rote = os.path.dirname(__file__)
    vv = Work_station(Type=id).Check()
    if vv is False:
        mm = exib_Original.DD(rote)
    else:
        mm = exib_Original.DD(rote,vv)
    if mm !=[]:
        for i in mm:
            name = i["歌名"]
            singer = i["演唱："]
            Lyric_ci = i["歌词"]
            Original_song = i["原唱："]
            Type = 2
            Address = i["地址"]
            if i["图片地址"] is None:
                photo = "images/moves/none_images.jpg"
            else:
                photo = i["图片地址"]
            time = str(i["上传时间："]).replace("上传时间：", "")
            aa = Work_station(name=name, singer=singer,Original_song=Original_song,
                         Lyric_ci=Lyric_ci, Type=Type, Address=Address, time=time,
                         photo=photo).Increase()

# 主页
@app.route('/')
@app.route('/<string:type>')
@app.route('/index')
def index(type=None):
    if session == {}:
        print(1)
    else:
        print("aaaaaaaaaa")
        mid = session['mid']
        username = session['username']
        result = user(user=username).Check()
        result_1 = center_u(mid=mid).Check()
        if result_1 == 'None':
            center_u(mid=mid, user=username).Increase()
            result_1 = center_u(mid=mid).Check()
        session['phone'] = str(result_1['phone'])
        session['nickname'] = str(result_1['nickname'])
        session['qq_number'] = str(result_1['qq_number'])
        session['last_login'] = str(result[1]['last_login'])
        session['group'] = str(result[1]['group'])


    # 轮播图
    re = Images_page(position=1).All()
    cap = index_Handle.index().Carousel_figure(re)
    # 信息展示
    page = request.args.get('page', 1, type=int)
    if type == "1":
        yuan_page = page
        fan_page = 1
    elif type == "2":
        yuan_page = 1
        fan_page = page
    else:
        yuan_page = page
        fan_page = page
    # 原创
    mp = Work_station(Type=1,page=yuan_page, total=4).All()
    # 翻唱
    mpp = Work_station(Type=2, page=fan_page, total=2).All()
    return render_template('index.html',bb=cap,posts=mp.items, pagination=mp,posts1=mpp.items, pagination1=mpp)

# @app.route('/index/<int:page>')
# def index_1(page):
#     # 轮播图
#     re = Images_page(position=1).All()
#     cap = index_Handle.index().Carousel_figure(re)
#
#     # 信息
#     bb = index_Handle.index(page).Display_information()
#     if bb is False:
#         return redirect(url_for('index'))
#     else:
#         return render_template('index.html',cap=bb, bb=cap)
# 登录页
@app.route('/login')
def login():
    return render_template('login.html')

@app.route('/login/login_check/', methods=['GET', 'POST'])
def login_l():
    if request.method == 'POST':
        username = request.form.get('username', '')
        password = request.form.get('password', '')
        if username == '' or password == '':
            response_data = {'state': 'fail', 'message': '手机号密码不能为空'}
            return jsonify(response_data)
        result = user(username, password).Check(number=1)
        if result[0] is True:
            user(user=result[1]['user']).Change()
            result_1 = center_u(result[1]['mid']).Check()
            if result_1 == 'None':
                center_u(result[1]['mid'], username).Increase()
                result_1 = center_u(result[1]['mid']).Check()
            session['mid'] = str(result_1['mid'])
            session['username'] = str(result_1['user'])
            session['nickname'] = str(result_1['nickname'])
            response_data = {'state': 'success', 'message': '登录成功', 'url': '/index'}
        else:
            response_data = {'state': 'fail', 'message': result[1]}
        return jsonify(response_data)
    else:
        return redirect(url_for('login'))

@app.route('/logon')
def logon():
    session.clear()
    return redirect(url_for('index'))

@app.route('/register')
def register():
    return render_template('register.html')

# 注册
@app.route('/register/register_check', methods=['GET', 'POST'])
def register_1():
    if request.method == 'POST':
        username = request.form.get('username', '')
        password = request.form.get('password', '')
        password_again = request.form.get('password_again', '')
        if username == '' or password == '':
            response_data = {'state': 'fail', 'message': '手机号或密码不能为空'}
            return jsonify(response_data)
        if password != password_again:
            response_data = {'state': 'fail', 'message': '两次密码不相等'}
            return jsonify(response_data)
        result = user(username, password).Increase()
        if result[0] is not True:
            response_data = {'state': 'fail', 'message': result[1]}
        else:
            center_u(result[1]['mid'], username).Increase()
            response_data = {'state': 'success', 'message': '注册成功', 'url': '/login'}
        return jsonify(response_data)
    else:
        return redirect(url_for('register'))

@app.route('/center')
def center():
    if session == {}:
        return redirect(url_for('index'))
    else:
        return render_template('center.html', captal=session)


@app.route('/center/center_check', methods=['GET', 'POST'])
def center_1():
    if request.method == "POST":
        # {'username': name, 'nickname': nickname, 'qq_number': qq_number, 'phone': phone}
        username = request.form.get('username', '')
        nickname = request.form.get('nickname', '')
        qq_number = request.form.get('qq_number','')
        phone = request.form.get('phone','')
        result = center_u(mid=session['mid'],user=username, phone=phone, nickname=nickname, qq_number=qq_number).Change()
        if result is not True:
            response_data = {'state': 'fail', 'message': result[1]}
        else:
            response_data = {'state': 'success', 'message': '提交成功', 'url': '/index'}
        return jsonify(response_data)
# 原创区
@app.route('/Original')
def Exhibition():

    page = request.args.get('page', 1, type=int)
    mp = Work_station(Type=1, page=page, total=8).All()
    return render_template('Exhibition_works.html',posts=mp.items, pagination=mp)

# 翻唱区
@app.route('/Cover')
def Exhibition_Cover():
    page = request.args.get('page', 1, type=int)
    mp = Work_station(Type=2, page=page, total=8).All()
    return render_template('Cover_works.html',posts=mp.items, pagination=mp)
# 点赞处理
@app.route('/Dispose', methods=["GET"])
def Dispose():
    id = request.args.get('id')
    meth = request.args.get('meth')
    mid = request.args.get('mid')
    if session =={}:
        response_data = {'state': 'fail', 'message': '请先登录'}
        return jsonify(response_data)
    else:
        ses = session["mid"]
        if mid != ses:
            response_data = {'state': 'fail', 'message': '请合理投票！'}
            return jsonify(response_data)
        elif id == "" or meth == "":
            response_data = {'state': 'fail', 'message': '请合理投票'}
            return jsonify(response_data)
    time1 = time.strftime("%Y-%m-%d")
    bs = Thumb_sp(Type=meth,mid=mid,time=time1).all_count(count=2)
    if bs >=5:
        response_data = {'state': 'fail', 'message': '已超过今日可投数量'}
    else:
        result = Thumb_sp(name_id=id,Type=meth,mid=mid,time=time1).Check()
        if result is False:
            Thumb_sp(name_id=id,Type=meth,mid=mid).Increase()
            result = Work_station(id=id).Check()
            response_data = {'state': 'success', 'message': '投票成功', 'url': result[1]["give_up"]}
        else:
            response_data = {'state': 'fail', 'message': '今日已投票，不可再投'}
    return jsonify(response_data)



# 后台

# 会员管理-用户
class MyView(BaseView):
    # 会员管理用户首页
    @expose('/')
    def index(self):
        search = request.args.get('search','')
        page = request.args.get('page', 1, type=int)
        if search != '':
            mr = user(search=search, page=page, total=5).All()
        else:
            page = request.args.get('page', 1, type=int)
            mr = user(page=page, total=5).All()
        return self.render('admin/admin_list.html', posts=mr.items, pagination=mr)

    # 增加 修改
    @expose('/<string:change>')
    def create(self,change):
        number = request.args.get('number','')
        if change != 'create' and change != 'change' and change != 'delete' and change !='check':
            return redirect(url_for('myview.index'))
        else:
            if change == 'change' or change == 'check':
                result = user(mid=number).Check()
                result_1 = [change,result[1]]
                return self.render('admin/admin_list_change.html', cha=result_1)
            elif change == 'delete':
                t = number.strip('["]')
                list_1 = t.split('","')
                for i in list_1:
                    result = user(mid=i).Delete()
                    if result is True:
                        center_u(mid=i).Delete()
                return redirect(url_for('myview.index'))
            else:
                result_1 = [change]
            return self.render('admin/admin_list_change.html', cha=result_1)
    # 增加修改数据处理
    @expose('/user_1/', methods=['GET', 'POST'])
    def create_1(self):
        if request.method =='POST':
            username = request.form.get('name', '')
            password = request.form.get('password', '')
            group = request.form.get('group', '')
            change = request.form.get('cha', '')
            if username == '':
                response_data = {'state': 'fail', 'message': '用户名不能为空'}
                return jsonify(response_data)
            if group != '1' and group != '2':
                response_data = {'state': 'fail', 'message': '请输入正确的分组'}
                return jsonify(response_data)
            # 增加
            if change == 'create':
                result = user(user=username, password=password, group=group).Increase()
                if result[0] is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    center_u(mid=result[1]['mid'], user=username).Increase()
                    response_data = {'state': 'success', 'message': '添加成功', 'url': '/admin/myview/'}
            # 修改
            elif change == 'change':
                result = user(user=username, last_user=group, changes='group').Change()
                if password != '':
                    result = user(user=username, last_user=password, changes='password').Change()
                if result is not True:
                    response_data = {'state': 'fail', 'message': '账号不存在'}
                else:
                    response_data = {'state': 'success', 'message': '修改成功', 'url': '/admin/myview/'}
            else:
                return redirect(url_for('myview.index'))
        else:
            return redirect(url_for('myview.index'))
        return jsonify(response_data)

#会员管理-组
class MyGroup(BaseView):
    @expose('/')
    def index(self):
        search = request.args.get('search','')
        page = request.args.get('page', 1, type=int)
        if search != '':
            mr = user(search=search, page=page, total=5).All()
        else:
            page = request.args.get('page', 1, type=int)
            mr = group(page=page, total=5).All()
        return self.render('admin/admin_group.html', posts=mr.items, pagination=mr)

    # 增加 修改 删除 查看
    @expose('/<string:change>')
    def create(self,change):
        number = request.args.get('number','')
        if change != 'create' and change != 'change' and change != 'delete' and change !='check':
            return redirect(url_for('mygroup.index'))
        else:
            if change == 'change' or change == 'check':
                result =  group(id=number).Check()
                result_1 = [change,result[1]]
                return self.render('admin/admin_group_change.html', cha=result_1)
            elif change == 'delete':
                t = number.strip('["]')
                list_1 = t.split('","')
                for i in list_1:
                    group(id=i).Delete()
                return redirect(url_for('mygroup.index'))
            else:
                result_1 = [change]
            return self.render('admin/admin_group_change.html', cha=result_1)

    # 增加修改删除查看数据处理
    @expose('/group_1/', methods=['GET', 'POST'])
    def create_1(self):
        if request.method =='POST':
            username = request.form.get('name', '')
            # last_name = request.form.get('last_name', '')
            id = request.form.get('id', '')
            change = request.form.get('cha', '')
            # 增加
            if change == 'create':
                result = group(name=username).Increase()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '添加成功', 'url': '/admin/mygroup/'}
            # 修改
            elif change == 'change':
                if username == '':
                    response_data = {'state': 'fail', 'message': '名称不能为空'}
                    return jsonify(response_data)
                result = group(id=id, last_name=username).Change()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '修改成功', 'url': '/admin/mygroup/'}
            else:
                return redirect(url_for('mygroup.index'))
        else:
            return redirect(url_for('mygroup.index'))
        return jsonify(response_data)


# 首页图片
class MyImage(BaseView):
    @expose('/')
    def index(self):
        search = request.args.get('search','')
        page = request.args.get('page', 1, type=int)
        if search != '':
            mr = user(search=search, page=page, total=5).All()
        else:
            mr = Images_page(page=page, total=5).All()
        return self.render('admin/admin_image_index.html', posts=mr.items, pagination=mr)
    #图片处理 首页轮播图
    @expose('/upload', methods=['POST'])
    def change_avatar(self):
        if request.method == 'POST':
            name = request.form.get('user','')
            file = request.files['file']
            id = request.form.get('id','')
            if file.filename == '':
                return redirect(url_for('myimage.index'))
            # 获取图片后缀
            a = file.filename.find('.')
            c = file.filename[a:]
            # 组合图片名字及地址
            d = 'images/' + name + c
            size = (1270, 250)
            im = Image.open(file)
            im.thumbnail(size)
            im.save(os.path.join(''.join([os.path.dirname(__file__), "/static/"+d])))
            result = Images_page(id=id,Route=d).Change()
            return redirect(url_for('myimage.index'))
        return render_template('admin/admin_image_index_change.html')
    # 图片管理-分页
    @expose('/<int:page>')
    def index_1(self, page):
        user = Models.Photo_index.query.all()
        mm = []
        b =[]
        for i in user:
            pp = {'id': i.id, 'name': i.name, 'last_login':i.last_login, 'Route':str(i.Route)}
            mm.append(pp)
        mp = mm
        while mm:
            cc = mp[:5]
            del mp[:5]
            b.append(cc)
        d = []
        for i in range(1, len(b) + 1):
            d.append(i)
        if page > len(d):
            return redirect(url_for('myimage.index'))
        else:
            cap = {'list': b, 'page': d, 'page_len': page - 1}
        return self.render('admin/admin_image_index.html', cap=cap)

    # 增加 修改 删除 查看
    @expose('/<string:change>')
    def create(self,change):
        number = request.args.get('number','')
        if change != 'create' and change != 'change' and change != 'delete' and change !='check':
            return redirect(url_for('myimage.index'))
        else:
            if change == 'change' or change == 'check':
                result =  Images_page(id=number).Check()
                result_1 = [change,result[1]]
                return self.render('admin/admin_image_index_change.html', cha=result_1)
            elif change == 'delete':
                t = number.strip('["]')
                list_1 = t.split('","')
                for i in list_1:
                    result = Images_page(id=number).Check()
                    mb = result[1]["Route"]
                    # 删除图片文件
                    if mb != "images/index_long_page.jpg":
                        oo = os.path.dirname(__file__)
                        od = oo.replace('\\','/')
                        mb_1 = '/static/' + mb
                        vv = ''.join([od, mb_1])
                        os.remove(vv)
                    Images_page(id=i).Delete()
                return redirect(url_for('myimage.index'))
            else:
                result_1 = [change]
            return self.render('admin/admin_image_index_change.html', cha=result_1)

    # 图片增加修改删除查看数据处理
    @expose('/myimage_1/', methods=['GET', 'POST'])
    def create_1(self):
        if request.method =='POST':
            username = request.form.get('name', '')
            change = request.form.get('cha', '')
            id = request.form.get('id', '')
            if username == '':
                response_data = {'state': 'fail', 'message': '名称不能为空'}
                return jsonify(response_data)
            # 增加
            if change == 'create':
                route = "images/index_long_page.jpg"
                result = Images_page(name=username, Route=route,position=1).Increase()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '添加成功', 'url': '/admin/myimage/'}
            # 修改
            elif change == 'change':
                result = Images_page(id=id, last_name=username).Change()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '修改成功', 'url': '/admin/myimage/'}
            else:
                return redirect(url_for('myimage.index'))
        else:
            return redirect(url_for('myimage.index'))
        return jsonify(response_data)

# 作品展示
class Workexib(BaseView):
    @expose("/")
    def index(self):
        search = request.args.get('search', '')
        page = request.args.get('page', 1, type=int)
        if search != '':
            mr = Work_station(search=search, page=page, total=5, Type=1).All()
        else:
            mr = Work_station(page=page, total=5, Type=1).All()
        return self.render("admin/admin_workexib.html", posts=mr.items, pagination=mr)

    #图片处理 原创海报
    @expose('/upload', methods=['POST'])
    def change_avatar(self):
        if request.method == 'POST':
            file = request.files['file']
            id = request.form.get('ids','')
            if file.filename == '':
                return redirect(url_for('workexib.index'))
            # 组合图片名字及地址
            c = "images/moves/yuan/" + str(int(time.time())) + ".jpg"
            im = Image.open(file)
            im.save(os.path.join(''.join([os.path.dirname(__file__), "/static/"+c])))
            result = Work_station(id=id,photo=c).Change()
            print("result:",result)
            return redirect(url_for('workexib.index'))
        return render_template('admin/admin_workexib_change.html')

    # 增加 修改 删除 查看
    @expose('/<string:change>')
    def create(self,change):
        number = request.args.get('number','')
        if change != 'create' and change != 'change' and change != 'delete' and change !='check':
            return redirect(url_for('workexib.index'))
        else:
            if change == 'change' or change == 'check':
                result =  Work_station(id=number).Check()
                result_1 = [change,result[1]]
                print("result:",result_1)
                return self.render('admin/admin_workexib_change.html', cha=result_1)
            elif change == 'delete':
                t = number.strip('["]')
                list_1 = t.split('","')
                for i in list_1:
                    result = Work_station(id=i).Check()
                    mb = result[1]["photo"]
                    print("mb:",mb)
                    if mb != "images/moves/none_images.jpg":
                        oo = os.path.dirname(__file__)
                        od = oo.replace('\\', '/')
                        mb_1 = '/static/' + mb
                        vv = ''.join([od, mb_1])
                        print("vv:",vv)
                        os.remove(vv)
                    Work_station(id=i).Delete()
                return redirect(url_for('workexib.index'))
            else:
                result_1 = [change]
            return self.render('admin/admin_workexib_change.html', cha=result_1)

    # 增加修改删除查看数据处理
    @expose('/workexib_1/', methods=['GET', 'POST'])
    def create_1(self):
        if request.method == 'POST':
            id = request.form.get('id','')
            name = request.form.get('name', '')
            singer = request.form.get('singer', '')
            Lyrics = request.form.get('Lyrics', '')
            Composer = request.form.get('Composer', '')
            Arranger = request.form.get('Arranger', '')
            Mix = request.form.get('Mix', '')
            Lyric = request.form.get('Lyric', '')
            address = request.form.get('address', '')
            type = request.form.get('type', '')
            change = request.form.get('cha', '')
            if name == '' or singer == '' or type == '':
                response_data = {'state': 'fail', 'message': '名称、歌手、类别不能为空'}
                return jsonify(response_data)
            # 增加
            if change == 'create':
                photo = "images/moves/none_images.jpg"
                result = Work_station(name=name,singer=singer,Lyrics=Lyrics,
                                Composer=Composer,Arranger=Arranger,Mix=Mix,
                                Lyric_ci=Lyric,Type=type,Address=address,photo=photo).Increase()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '添加成功', 'url': '/admin/workexib/'}
            # 修改
            elif change == 'change':
                result = Work_station(id=id, name=name,singer=singer,Lyrics=Lyrics,
                                Composer=Composer,Arranger=Arranger,Mix=Mix,
                                Lyric_ci=Lyric,Type=type,Address=address).Change()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '修改成功', 'url': '/admin/workexib/'}
            else:
                return redirect(url_for('workexib.index'))
        else:
            return redirect(url_for('workexib.index'))
        return jsonify(response_data)
# 查看歌词
    @expose("/Lyric")
    def create_2(self):
        id = request.args.get('Lyric', '')
        result = Work_station(id=id).Check()
        aa = result[1]["Lyric_ci"]
        if aa == '':
            response_data = {'message': "暂无歌词"}
        else:
            response_data = {'message': aa}
        return jsonify(response_data)

    @expose("/orgina",methods=["POST"])
    def orgina(self):
        pass


# 作品展示-翻唱
class Cover(BaseView):
    @expose("/")
    def index(self):
        search = request.args.get('search', '')
        page = request.args.get('page', 1, type=int)
        if search != '':
            mr = Work_station(search=search, page=page, total=5, Type=2).All()
        else:
            mr = Work_station(page=page, total=5, Type=2).All()
        return self.render("admin/admin_cover.html", posts=mr.items, pagination=mr)

    #图片处理 翻唱海报
    @expose('/upload', methods=['POST'])
    def change_avatar(self):
        if request.method == 'POST':
            file = request.files['file']
            id = request.form.get('ids','')
            if file.filename == '':
                return redirect(url_for('cover.index'))
            # 组合图片名字及地址
            c = "images/moves/fan/" + str(int(time.time())) + ".jpg"
            im = Image.open(file)
            im.save(os.path.join(''.join([os.path.dirname(__file__), "/static/"+c])))
            result = Work_station(id=id,photo=c).Change()
            return redirect(url_for('cover.index'))
        return render_template('admin/admin_cover_change.html')

    # 增加 修改 删除 查看
    @expose('/<string:change>')
    def create(self, change):
        number = request.args.get('number', '')
        if change != 'create' and change != 'change' and change != 'delete' and change != 'check':
            return redirect(url_for('cover.index'))
        else:
            if change == 'change' or change == 'check':
                result = Work_station(id=number).Check()
                result_1 = [change, result[1]]
                return self.render('admin/admin_cover_change.html', cha=result_1)
            elif change == 'delete':
                t = number.strip('["]')
                list_1 = t.split('","')
                for i in list_1:
                    result = Work_station(id=i).Check()
                    mb = result[1]["photo"]
                    if mb != "images/moves/none_images.jpg":
                        oo = os.path.dirname(__file__)
                        od = oo.replace('\\', '/')
                        mb_1 = '/static/' + mb
                        vv = ''.join([od, mb_1])
                        os.remove(vv)
                    Work_station(id=i).Delete()
                return redirect(url_for('cover.index'))
            else:
                result_1 = [change]
            return self.render('admin/admin_cover_change.html', cha=result_1)

    # 增加修改删除查看数据处理
    @expose('/cover_1/', methods=['GET', 'POST'])
    def create_1(self):
        if request.method == 'POST':
            id = request.form.get('id', '')
            name = request.form.get('name', '')
            singer = request.form.get('singer', '')
            Original_song = request.form.get('Original_song', '')
            Lyric = request.form.get('Lyric', '')
            address = request.form.get('address', '')
            type = request.form.get('type', '')
            change = request.form.get('cha', '')
            if name == '' or singer == '' or type == '':
                response_data = {'state': 'fail', 'message': '名称、歌手、类别不能为空'}
                return jsonify(response_data)
            # 增加
            if change == 'create':
                photo = "images/moves/none_images.jpg"
                result = Work_station(name=name, singer=singer, Original_song=Original_song,
                                      Lyric_ci=Lyric, Type=type, Address=address,photo=photo).Increase()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '添加成功', 'url': '/admin/cover/'}
            # 修改
            elif change == 'change':
                result = Work_station(id=id, name=name, singer=singer, Original_song=Original_song,
                                      Lyric_ci=Lyric, Type=type, Address=address).Change()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '修改成功', 'url': '/admin/workexib/'}
            else:
                return redirect(url_for('cover.index'))
        else:
            return redirect(url_for('cover.index'))
        return jsonify(response_data)
    # 查看歌词
    @expose("/Lyric")
    def create_2(self):
        id = request.args.get('Lyric', '')
        result = Work_station(id=id).Check()
        aa = result[1]["Lyric_ci"]
        if aa == '':
            response_data = {'message': "暂无歌词"}
        else:
            response_data = {'message': aa}
        return jsonify(response_data)


    @expose("/orgina", methods=["POST"])
    def orgina(self):
        pass
# 脚本
class Script_ben(BaseView):
    @expose("/")
    def index(self):
        page = request.args.get('page', 1, type=int)
        mr = Implement(page=page, total=5).All()
        return self.render("admin/admin_script.html", posts=mr.items, pagination=mr)

    # 增加 修改 删除 查看
    @expose('/<string:change>')
    def create(self,change):
        number = request.args.get('number','')
        if change != 'create' and change != 'change' and change != 'delete' and change !='check':
            return redirect(url_for('Script_ben.index'))
        else:
            if change == 'change' or change == 'check':
                result =  Implement(id=number).Check()
                result_1 = [change,result[1]]
                return self.render('admin/admin_script_change.html', cha=result_1)
            elif change == 'delete':
                t = number.strip('["]')
                list_1 = t.split('","')
                for i in list_1:
                    Implement(id=i).Delete()
                return redirect(url_for('script_ben.index'))
            else:
                result_1 = [change]
            return self.render('admin/admin_script_change.html', cha=result_1)

    # 增加修改删除查看数据处理
    @expose('/script_ben_1/', methods=['GET', 'POST'])
    def create_1(self):
        if request.method =='POST':
            username = request.form.get('name', '')
            Route = request.form.get('Route', '')
            id = request.form.get('id', '')
            change = request.form.get('cha', '')
            # 增加
            if change == 'create':
                result = Implement(name=username,Route=Route).Increase()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '添加成功', 'url': '/admin/script_ben/'}
            # 修改
            elif change == 'change':
                if username == '':
                    response_data = {'state': 'fail', 'message': '名称不能为空'}
                    return jsonify(response_data)
                result = Implement(id=id, name=username,Route=Route).Change()
                if result is not True:
                    response_data = {'state': 'fail', 'message': result[1]}
                else:
                    response_data = {'state': 'success', 'message': '修改成功', 'url': '/admin/script_ben/'}
            else:
                return redirect(url_for('script_ben.index'))
        else:
            return redirect(url_for('script_ben.index'))
        return jsonify(response_data)
    @expose("script/")
    def test(self):
        id = request.args.get('id')
        if id == "1":
            Yuan(id)
        elif id == "2":
            Fan(id)
        response_data = {'state': 'success', 'message': '修改成功', 'url': '/admin/script_ben/'}
        return jsonify(response_data)


class MyAdmin_user(ModelView):
    @expose('/new/', methods=['GET'])
    def create_view(self):
        return self.render('admin_index.html')
    # 显示中文
    column_labels = {'mid':'MID','user':'用户名','group':'组'}
    # 是否允许创建
    can_create = False
    # 这三个变量定义管理员是否可以增删改，默认为True
    # can_delete = False
    # can_edit = False
    # can_create = False
    #这里是为了自定义显示的column名字
    # column_labels = dict( username=u'用户名', )
    #如果不想显示某些字段，可以重载这个变量
    # column_exclude_list = ( 'password_hash', )
    # 显示的字段
    column_list = ('mid', 'user', 'group')
    # 过滤器
    column_filters = {'mid':'MID', 'user':'用户名'}
    # 可搜索的字段
    column_searchable_list = {'mid':'MID', 'user':'用户名'}
    def is_accessible(self):
        return redirect(url_for('login', next=request.url))
class MyAdmin_groups(ModelView):
    column_labels = {'id': 'ID', 'name': '组名'}
    can_create = True
    column_list = ('id', 'name')

